package threadDemo1.test_synchronized;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-28
 * Time: 15:26
 */
public class ThreadDemo {
    //锁对象
    private static final Object locker = new Object();
    private static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 500000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 500000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("count = " + count);//1000000
    }
}
